package com.central.apps.common.util;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Map自定义处理类
 *
 * @author 李大超
 * @date 2023/4/23
 */
public class MapCustomUtils {

    /**
     * 比较两个map是否不同
     *
     * @param fromMap
     * @param toMap
     * @return
     */
    public static boolean compareTask(Map<String, Object> fromMap, Map<String, Object> toMap, List<String> expFields) {
        if (CollectionUtils.isEmpty(expFields)) {
            expFields = new ArrayList<>();
        }

        // 默认排除字段
        expFields.add("id");
        expFields.add("updatetime");
        expFields.add("updateuser");
        expFields.add("status");
        expFields.add("version");
        expFields.add("createuser");
        expFields.add("createuserdesc");
        expFields.add("createtime");
        expFields.add("deptid");
        expFields.add("deptcode");

        boolean bool = false;
        for (Map.Entry<String, Object> fromEntry : fromMap.entrySet()) {
            if (expFields.contains(fromEntry.getKey())) {
                continue;
            }

            for (Map.Entry<String, Object> toEntry : toMap.entrySet()) {
                if (fromEntry.getKey().equals(toEntry.getKey()) && !fromEntry.getValue().equals(toEntry.getValue())) {
                    toMap.put(toEntry.getKey(), fromEntry.getValue());

                    if (!bool) {
                        bool = true;
                    }
                }
            }
        }

        return bool;
    }

    /**
     * 比较值是否相等
     *
     * @param obj1
     * @param obj2
     * @return
     */
    public static boolean compareMapValue(Object obj1, Object obj2) {
        if (obj1 != null && obj1.equals(obj2)) {
            return true;
        }

        return false;
    }

    /**
     * 根据Map数据重新组织数据
     *
     * @param initMap
     * @return
     */
    public static Map<String, Object> combineMap(Map<String, Object> initMap, List<String> filterFields) {
        Map<String, Object> map = new HashMap<>();
        for (Map.Entry<String, Object> entry : initMap.entrySet()) {
            if (CollectionUtils.isNotEmpty(filterFields)) {
                if (filterFields.contains(entry.getKey())) {
                    continue;
                }
            }

            map.put(entry.getKey(), entry.getValue());
        }

        return map;
    }

    /**
     * 组织子表数据list
     *
     * @param data
     * @return
     */
    public static List<Map<String, Object>> combineChildList(Map<String, Object> data, String uniqueId) {
        Object object = MapUtils.getObject(data, uniqueId);
        Object chgObject = MapUtils.getObject(data, uniqueId + "ChangeData");

        if (object != null) {
            List<Map<String, Object>> childList = (List<Map<String, Object>>) object;

            if (chgObject != null) {
                List<Map<String, Object>> chgChildList = (List<Map<String, Object>>) chgObject;
                for (Map<String, Object> chgChildMap : chgChildList) {
                    for (Map<String, Object> childMap : childList) {
                        if (chgChildMap.get("id").equals(childMap.get("id"))) {
                            childList.remove(childMap);
                            childList.add(chgChildMap);
                            break;
                        }
                    }
                }
            }

            return childList;
        }

        return null;
    }

    /**
     * 组织子表数据list
     *
     * @param data
     * @return
     */
    public static List<Map<String, Object>> combineChildChgList(Map<String, Object> data, String uniqueId) {
        Object chgObject = MapUtils.getObject(data, uniqueId + "ChangeData");

        if (chgObject != null) {
            return (List<Map<String, Object>>) chgObject;
        }

        return null;
    }
}
